/***
 *   创建订单相应接口
 *   Created by lihao on 2018-04-17
 */

let express = require('express');
let router = express.Router();
let db = require('../conf');
let tools = require('../util');

// 获取订单管理列表
router.post('/getList',function(req,res){
    const n = req.body.pageVo.pageNo;
    const m = req.body.pageVo.pageSize;
    const x = (n-1)*m;
    const c = req.body.condition;
    console.log(req.body)
    c.orderStatus = 'NEW';
    let c_and = '';
    const b = req.body.pageVo;

    // 格式化条件
    for(k in c){
        if(c[k]){
            c_and += (k+'='+'"'+c[k]+'" AND ');
        }
    }
    if(c_and){
        c_and = c_and.substring(0,c_and.length-5)
    }

    const sql = 'SELECT *,by_order.id as mainId,by_bbd_customer.value AS customerName,by_bbd_customer.phone AS customerPhone,by_bbd_freight.value AS freight,by_bbd_freight.phone AS freightPhone FROM by_order INNER JOIN by_bbd_customer ON by_order.customer_id = by_bbd_customer.id INNER JOIN by_bbd_freight ON by_order.freight_id = by_bbd_freight.id WHERE '+ c_and +' ORDER BY mainId DESC LIMIT ?,?;SELECT COUNT(id) as total FROM by_order WHERE '+ c_and;


    db(sql,[x,m]).then(data => {
        b.total = data[1][0].total;
        res.send({
            code:0,
            success:true,
            message: '获取列表成功',
            data: data[0],
            pageVo: b
        })
    }).catch(err =>{
        res.send({
            code:10003,
            success:false,
            message: '系统错误，请联系管理员',
            data: {},
            pageVo: b
        })
    })


});

// 根据系统订单号查询详情
router.post('/getDetail',function(req,res){
    const n = req.body.orderId;
    const temp = ['chair','table','tea','tv','remark'];
    const arr = [];
    let sql = '',resData = {};

    for(let i = 0; i< temp.length; i++){
        sql+= 'SELECT * FROM by_order_'+temp[i]+' WHERE orderId = ?;';
        arr.push(n)
    }

    db(sql,arr).then(data => {

        for(let n =0; n< temp.length; n++){
            resData[temp[n]] = data[n]
        }

        res.send({
            code: 0,
            success: true,
            message: '获取订单详情成功',
            data: resData
        })
    }).catch(err=>{
        res.send({
            code: 10030,
            success: false,
            message: err,
            data: resData
        })
    })



});

// 发运订单
router.post('/delivery',function(req,res){
    const n = req.body.orderId.toString().split(',');
    const sql = 'UPDATE by_order SET orderStatus ="DELIVERED" WHERE orderId in (?)';

    db(sql,[n]).then(data => {

        res.send({
            code: 0,
            success: true,
            message: '订单发运成功',
            data:{}
        })
    })
});

// 取消发运
router.post('/cancelDelivery',function(req,res){
    const n = req.body.orderId;
    const sql = 'UPDATE by_order SET orderStatus ="NEW" WHERE orderId=?';

    db(sql,[n]).then(data => {

        res.send({
            code: 0,
            success: true,
            message: '取消订单发运成功',
            data:{}
        })
    })
});

// 逻辑删除订单
router.post('/deleteDelivery',function(req,res){
    const n = req.body.orderId.split(',');
    const sql = 'UPDATE by_order SET orderStatus ="DELETE" WHERE orderId in (?)';

    db(sql,[n]).then(data => {

        res.send({
            code: 0,
            success: true,
            message: '删除订单成功',
            data:{}
        })
    })
});

// 已付款
router.post('/payOff',function(req,res){
    const n = req.body.orderId;
    const sql = 'UPDATE by_order SET isPay =1 WHERE orderId=?';

    db(sql,[n]).then(data => {

        res.send({
            code: 0,
            success: true,
            message: '此单已付款',
            data:{}
        })
    })
});


module.exports = router